package com.tjtt.tdm.form.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.tjtt.common.utils.StringUtil;
import com.tjtt.tdm.base.BaseAction;
import com.tjtt.tdm.base.PageResult;
import com.tjtt.tdm.flow.bean.TdmCustomNo;
import com.tjtt.tdm.form.bean.AutoFormsButton;
import com.tjtt.tdm.form.bean.AutoFormsField;
import com.tjtt.tdm.form.bean.AutoFormsNew;
import com.tjtt.tdm.form.service.IAutoFormService;

public class AutoFormActionNew extends BaseAction{

	private IAutoFormService autoFormService;
	
	private String formCnName;
	private String formEnName;
	private String formPath;
	private String formNote;
	private Long formColumn;
	private String formFormat;
	private Long createNameId;
	private String createName;
	private Timestamp createTime;
	private String formFormatDate;
	private String formType;
	private String suoslx;
	private String formwidth;
	private String formheight;
	private String iscaiji;
	private String caijiaddress;
	
	private String formId;
	private String cnName;
	private String fieldName;
	private String fieldNote;
	private String fieldType;
	private Long isReadOnly;
	private String authenticationType;
	private Long isListShow;
	private Long isPageShow;
	private Long showSeq;
	private Long isColumnMerge;
	private String defaultValue;
	private Long isQueryField;
	private Long fieldLength;
	private Long fieldListWidth;
	private Long isOrderBy;
	private Long pageDisplayType;
	private String foreignTableName;
	private String foreignCloumn;
	private String foreignDisCloumn;
	private Long foreignTableId;
	private String customNoName;
	private Long customNoId;
	private String eventType;
	private String eventName;
	private String eventFunction;
	
	private String formTableName;
	
	private String buttonName;
	private String byTableNameId;
	private String byTableColumn;
	private String thisTableNameId;
	private String thisTableColumn;
	private String fitterCondition;
	private Long buttonType;
	private Long operateType;
	private String columnCnName;
	private String columnSql;
	private String columnWidth;
	private String columnForigen;
	private String specialColumn;
	private Long specialType;
	private String otherColumnSql;
	private String columnguolvsql;
	private Long sfhebing;
	private Long isshowmap;
	private String mapmark;
	private Long marktype;
	private String bjingdu;
	private String bweidu;
	private String ejingdu;
	private String eweidu;
	public Long getMarktype() {
		return marktype;
	}
	public void setMarktype(Long marktype) {
		this.marktype = marktype;
	}
	public String getBjingdu() {
		return bjingdu;
	}
	public void setBjingdu(String bjingdu) {
		this.bjingdu = bjingdu;
	}
	public String getBweidu() {
		return bweidu;
	}
	public void setBweidu(String bweidu) {
		this.bweidu = bweidu;
	}
	public String getEjingdu() {
		return ejingdu;
	}
	public void setEjingdu(String ejingdu) {
		this.ejingdu = ejingdu;
	}
	public String getEweidu() {
		return eweidu;
	}
	public void setEweidu(String eweidu) {
		this.eweidu = eweidu;
	}
	public Long getIsshowmap() {
		return isshowmap;
	}
	public void setIsshowmap(Long isshowmap) {
		this.isshowmap = isshowmap;
	}
	/**
	 * 查询列表
	 * @throws Exception
	 */
	public void queryAutoFormListNewData() throws Exception{
		try {
			
			Map<String, String> filterMap = getPagerMap();
			String formsCnName = getRequest().getParameter("formsCnName");
			if(formsCnName != null && !"".equals(formsCnName)){
				formsCnName = StringUtil.encodingUrl(formsCnName);
			}
			String formsEnName = getRequest().getParameter("formsEnName");
			if(formsEnName != null && !"".equals(formsEnName)){
				formsEnName = StringUtil.encodingUrl(formsEnName);
			}
			String type = getRequest().getParameter("type");
			//将查询条件保存到Map里
			filterMap.put("form_cn_name", formsCnName);
			filterMap.put("form_en_name", formsEnName);
			filterMap.put("type", type);
			String[] viewArray = { "ID", "form_cn_name","form_en_name","SUOSLX","form_path","form_note","form_column","form_format","create_name","create_time","is_delete_table:[{'0':'是','1':'否'}]"};
			// 查询所有的数据
			PageResult result = autoFormService.queryAutoFormsListNewData(filterMap);
			String returnStr = getColumnJson(result, viewArray);
			setContentType();
			PrintWriter out = getResponse().getWriter();
			//System.out.println("--!"+returnStr+"--!");
			System.out.println(returnStr);
			out.print(returnStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void queryAutoFormListNew() throws Exception{
		try {
			
			Map<String, String> filterMap = getPagerMap();
			String formsCnName = getRequest().getParameter("formsCnName");
			if(formsCnName != null && !"".equals(formsCnName)){
				formsCnName = StringUtil.encodingUrl(formsCnName);
			}
			String formsEnName = getRequest().getParameter("formsEnName");
			if(formsEnName != null && !"".equals(formsEnName)){
				formsEnName = StringUtil.encodingUrl(formsEnName);
			}
			String type = getRequest().getParameter("type");
			//将查询条件保存到Map里
			filterMap.put("form_cn_name", formsCnName);
			filterMap.put("form_en_name", formsEnName);
			filterMap.put("type", type);
			String[] viewArray = { "ID", "form_cn_name","form_en_name","SUOSLX","form_path","form_note","form_column","form_format","create_name","create_time","is_delete_table:[{'0':'是','1':'否'}]"};
			// 查询所有的数据
			PageResult result = autoFormService.queryAutoFormsListNew(filterMap);
			String returnStr = getColumnJson(result, viewArray);
			setContentType();
			PrintWriter out = getResponse().getWriter();
			//System.out.println("--!"+returnStr+"--!");
			System.out.println(returnStr);
			out.print(returnStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 复制详情表
	 * @throws Exception
	 */
	public void insertDetailTable() throws Exception{
		PrintWriter out = getResponse().getWriter();
		try {
			String formEnName = getRequest().getParameter("formEnName");
			//判断是否已经复制过表
			String existsTableSql = "select count(1) from user_tables where table_name=upper('"+formEnName+"_b')";
			List list = autoFormService.getDatabySql(existsTableSql);
			if(Long.parseLong(list.get(0).toString()) > 0){
				out.print("{\"result\":\"existsTbale\"}");
			}else{
				autoFormService.insertDetailTable(formEnName);
				out.print("{\"result\":\"success\"}");
			}
		} catch (Exception e) {
			e.printStackTrace();
			out.print("{\"result\":\"error\"}");
		}
	}
	/**
	 * 复制表
	 * @author zhn
	 * @date Mar 3, 2014 5:38:53 PM
	 * @throws Exception
	 */
	public void insertTable() throws Exception{
		PrintWriter out = getResponse().getWriter();
		try {
			String formId = getRequest().getParameter("formId");
			String tableName = getRequest().getParameter("tableName");
			String tableCnName = getRequest().getParameter("tableCnName");
			String formEnName = getRequest().getParameter("formEnName");
			
			//判断是否已经复制过表
			String existsTableSql = "select count(1) from user_tables where table_name=upper('"+tableName+"')";
			List list = autoFormService.getDatabySql(existsTableSql);
			if(Long.parseLong(list.get(0).toString()) > 0){
				out.print("{\"result\":\"existsTbale\"}");
			}else{
				autoFormService.insertTable(formId,tableCnName,tableName,formEnName);
				//SEQ_AUTO_FROMS
				
				out.print("{\"result\":\"success\"}");
			}
		} catch (Exception e) {
			e.printStackTrace();
			out.print("{\"result\":\"error\"}");
		}
	}
	public void queryAutoFormsField() throws Exception{
		try {
			
			Map<String, String> filterMap = getPagerMap();
			String formsId = getRequest().getParameter("formsId");
			
			String fieldCnName = getRequest().getParameter("field_cn_name");
			if(fieldCnName != null && !"".equals(fieldCnName)){
				fieldCnName = StringUtil.encodingUrl(fieldCnName);
			}
			String fieldEnName = getRequest().getParameter("field_en_name");
			if(fieldEnName != null && !"".equals(fieldEnName)){
				fieldEnName = StringUtil.encodingUrl(fieldEnName);
			}
			String type = getRequest().getParameter("type");
			//将查询条件保存到Map里
			filterMap.put("formsId", formsId);
			filterMap.put("field_cn_name", fieldCnName);
			filterMap.put("field_en_name", fieldEnName);
			filterMap.put("type", type);
			String[] viewArray = { "ID", "field_cn_name","field_en_name"};
			// 查询所有的数据
			PageResult result = autoFormService.queryAutoFormsField(filterMap);
			String returnStr = getColumnJson(result, viewArray);
			setContentType();
			PrintWriter out = getResponse().getWriter();
			//System.out.println("--!"+returnStr+"--!");
			System.out.println(returnStr);
			out.print(returnStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 查询表字段列表
	 * @throws Exception
	 */
	
	public void queryAutoFormFieldListDicByFormId() throws Exception{
		try {
			
			Map<String, String> filterMap = getPagerMap();
			String formsId = getRequest().getParameter("formsId");
			String searchType=getRequest().getParameter("searchType");
			//将查询条件保存到Map里
			filterMap.put("forms_id", formsId);
			filterMap.put("searchType", searchType);
			String[] viewArray = { "ID", "cn_name","field_name","field_note","dictionarytable","field_type","is_read_only:[{'0':'否','1':'是'}]","authentication_type:[{'0':'无','1':'必填'}]","is_list_show:[{'0':'否','1':'是'}]",
					"is_page_show:[{'0':'否','1':'是'}]","show_seq","is_column_merge:[{'0':'否','1':'是'}]","field_list_width","is_order_by:[{'0':'否','1':'是'}]","default_value"};
			// 查询所有的数据
			PageResult result = autoFormService.queryAutoFormsFieldListDicByFormId(filterMap);
			String returnStr = getColumnJson(result, viewArray);
			setContentType();
			PrintWriter out = getResponse().getWriter();
			//System.out.println("--!"+returnStr+"--!");
			System.out.println(returnStr);
			out.print(returnStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	public void queryAutoFormFieldListByFormId() throws Exception{
		try {
			
			Map<String, String> filterMap = getPagerMap();
			String formsId = getRequest().getParameter("formsId");
			//将查询条件保存到Map里
			filterMap.put("forms_id", formsId);
			String[] viewArray = { "ID", "cn_name","field_name","field_note","field_type","is_read_only:[{'0':'否','1':'是'}]","authentication_type:[{'0':'无','1':'必填'}]","is_list_show:[{'0':'否','1':'是'}]",
					"is_page_show:[{'0':'否','1':'是'}]","show_seq","is_column_merge:[{'0':'否','1':'是'}]","field_list_width","is_order_by:[{'0':'否','1':'是'}]","default_value"};
			// 查询所有的数据
			PageResult result = autoFormService.queryAutoFormsFieldListByFormId(filterMap);
			String returnStr = getColumnJson(result, viewArray);
			setContentType();
			PrintWriter out = getResponse().getWriter();
			//System.out.println("--!"+returnStr+"--!");
			System.out.println(returnStr);
			out.print(returnStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 保存autoFrom对象（添加新表）
	 * @throws Exception
	 */
	public void saveAutoFormsNew() throws Exception{
		PrintWriter out = getResponse().getWriter();
		try {
			String userName = this.getUserName();
			String userId = this.getUserId();
			formEnName = formEnName.replace(" ", "");
			List listOld = autoFormService.queryAutoFormListByFormEnName(formEnName);
			if(listOld.size() == 0){
				AutoFormsNew af = new AutoFormsNew();
				af.setFormCnName(formCnName);
				af.setFormEnName(formEnName.toUpperCase());
				af.setFormPath(formPath);
				af.setFormNote(formNote);
				af.setFormColumn(formColumn);
				af.setSuoslx(suoslx);
				af.setFormwidth(formwidth);
				af.setFormheight(formheight);
				af.setCreateName(userName);
				af.setCreateNameId(Long.parseLong(userId));
				af.setCreateTime(new Timestamp(System.currentTimeMillis()));
				af.setFormType(formType);
				af.setCaijiaddress(caijiaddress);
				af.setIscaiji(iscaiji);
				af.setIsDeleteTable(0l);
				af.setColumnCnName(columnCnName);
				af.setColumnSql(columnSql);
				af.setColumnWidth(columnWidth);
				af.setColumnForigen(columnForigen);
				af.setSpecialColumn(specialColumn);
				af.setOtherColumnSql(otherColumnSql);
				af.setSpecialType(specialType);
				af.setColumnguolvsql(columnguolvsql);
				af.setIsshowmap(isshowmap);
				af.setMapmark(mapmark);
				autoFormService.insertCreateTableSql(af,formEnName);
				out.print("{\"result\":\"success\"}");
			}else{
				//表已存在
				out.print("{\"result\":\"errorExist\"}");
			}
		} catch (Exception e) {
			out.print("{\"result\":\"error\"}");
			e.printStackTrace();
		}
	}
	/**
	 * 查询此表中是否存在此字段名称
	 * @author zhn
	 * @date Feb 17, 2014 3:20:48 PM
	 * @throws IOException
	 */
	public void findFieldNameExec() throws IOException {
		PrintWriter out = getResponse().getWriter();
		try {	
			
			String formId1 = getRequest().getParameter("formId");
			String fieldName = StringUtil.encodingUrl(getRequest().getParameter("fieldName"));
			String fieldName1 = autoFormService.findFieldName(fieldName.toUpperCase(),formId1);
			if (Long.parseLong(fieldName1)==0) {
				out.print("{\"resultes\":\"success\"}");
			} else {
				out.print("{\"resultes\":\"false\"}");
			}
		} catch (Exception e) {
			e.printStackTrace();
			out.print("{\"result\":\"false\"}");
		}
	}
	/**
	 * 修改对象autoform
	 * @throws Exception
	 */
	public void updateAutoFormById() throws Exception{
		PrintWriter out = getResponse().getWriter();
		try {
			AutoFormsNew af = autoFormService.getAutoFormsNewById(formId);
			af.setFormNote(formNote);
			af.setFormColumn(formColumn);
			af.setSuoslx(suoslx);
			af.setFormwidth(formwidth);
			af.setFormheight(formheight);
			af.setColumnCnName(columnCnName);
			af.setColumnSql(columnSql);
			af.setColumnWidth(columnWidth);
			af.setColumnForigen(columnForigen);
			af.setSpecialColumn(specialColumn);
			af.setOtherColumnSql(otherColumnSql);
			af.setSpecialType(specialType);
			af.setColumnguolvsql(columnguolvsql);
			af.setIsshowmap(isshowmap);
			af.setMapmark(mapmark);
			af.setMarktype(marktype);
			af.setBjingdu(bjingdu);
			af.setBweidu(bweidu);
			af.setEjingdu(ejingdu);
			af.setEweidu(eweidu);
			autoFormService.updateAutoForm(af);
			out.print("{\"result\":\"success\"}");
		} catch (Exception e) {
			e.printStackTrace();
			out.print("{\"result\":\"error\"}");
		}
	}
	
	/**
	 * 根据传过来的id删除表，序列，数据
	 * @throws Exception
	 */
	public void deleteTableAndSeqAndAutoDate() throws Exception{
		PrintWriter out = getResponse().getWriter();
		try {
			String formId = getRequest().getParameter("formId");
			String formName = getRequest().getParameter("formName");
			autoFormService.deleteTableAndSeqAndDateByFormName(formName, formId);
			out.print("{\"result\":\"success\"}");
		} catch (Exception e) {
			e.printStackTrace();
			out.print("{\"result\":\"error\"}");
		}
	}
	
	/**
	 * 保存autoFrom对象（添加已存在表）
	 * @throws Exception
	 */
	public void saveAutoFormsAndOldTable() throws Exception{
		PrintWriter out = getResponse().getWriter();
		try {
			String formEnName = getRequest().getParameter("formEnName");
			String formCnName = getRequest().getParameter("formCnName");
			String userName = this.getUserName();
			String userId = this.getUserId();
			Object [] oldTableColumn = new Object[4];
			String formId = getRequest().getParameter("formId");

			List listOldTable = autoFormService.getTableColumnAndCommentAndDataType(formEnName);
			AutoFormsNew af = new AutoFormsNew();
			af.setFormCnName(formCnName);
			af.setFormEnName(formEnName);
			af.setFormColumn(1l);
			af.setFormwidth("500");
			af.setFormheight("500");
			af.setCreateName(userName);
			af.setCreateNameId(Long.parseLong(userId));
			af.setCreateTime(new Timestamp(System.currentTimeMillis()));
			af.setIsDeleteTable(1l);
			String autoFromId = autoFormService.saveAutoFormsNew(af);
			for(int i = 0; i < listOldTable.size(); i++ ){
				
				AutoFormsField aff = new AutoFormsField();
				oldTableColumn = (Object [])listOldTable.get(i);
				aff.setCnName(("".equals(oldTableColumn[2]) || oldTableColumn[2] == null)?"主键":oldTableColumn[2].toString());
				aff.setFieldName(("".equals(oldTableColumn[1]) || oldTableColumn[1] == null)?"":oldTableColumn[1].toString());
				aff.setFieldType(oldTableColumn[3].toString());
				long fieldLength;
				if(oldTableColumn[3].toString().equals("DATE"))
					
				{
					fieldLength=100;
				}
				else
				{
					fieldLength=Long.parseLong(oldTableColumn[4].toString());
				}
				//aff.setFieldLength(Long.parseLong(oldTableColumn[4].toString()));
				aff.setFieldLength(fieldLength);
				aff.setFieldListWidth(Long.parseLong(oldTableColumn[4].toString()));
				aff.setFormsId(Long.parseLong(autoFromId));
				aff.setIsReadOnly(0l);
				aff.setIsListShow(1l);
				aff.setIsPageShow(1l);
				autoFormService.saveAutoFormsField(aff);
			}
			out.print("{\"result\":\"success\"}");

			
		} catch (Exception e) {
			out.print("{\"result\":\"error\"}");
			e.printStackTrace();
		}
	}
	
	/**
	 * 查看
	 * @return
	 * @throws Exception
	 */
	public String getAutoFormInfoNew() throws Exception{
		try {
			String autoFormId = getRequest().getParameter("autoFormId");
			String type = getRequest().getParameter("typeId"); //如果是1，则查看，2则修改
			AutoFormsNew af = autoFormService.getAutoFormsNewById(autoFormId);
			getRequest().setAttribute("af", af);
			if("1".equals(type)){
				return "toView";
			}else{
				return "toUpdate";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
	
	/**
	 * 添加字段
	 * @throws Exception
	 */
	public void saveAutoFormField() throws Exception{
		PrintWriter out = getResponse().getWriter();
		try {
			AutoFormsField aff = new AutoFormsField();
			aff.setCnName(cnName);
			aff.setFieldName(fieldName.toUpperCase());
			aff.setFieldNote(fieldNote);
			aff.setFieldType(fieldType);
			aff.setIsReadOnly(isReadOnly);
			if(authenticationType != null && !"".equals(authenticationType)){
				aff.setAuthenticationType(authenticationType);
			}else{
				aff.setAuthenticationType("0");
			}
			aff.setIsListShow(isListShow);
			aff.setIsPageShow(isPageShow);
			aff.setIsQueryField(isQueryField);
			aff.setShowSeq(showSeq);
			aff.setDefaultValue(defaultValue);
			aff.setFormsId(Long.parseLong(formId));
			aff.setFieldLength(fieldLength);
			aff.setFieldListWidth(fieldListWidth);
			aff.setIsOrderBy(isOrderBy);
			aff.setPageDisplayType(pageDisplayType);
			aff.setForeignCloumn(foreignCloumn);
			aff.setForeignDisCloumn(foreignDisCloumn);
			aff.setForeignTableName(foreignTableName);
			aff.setForeignTableId(foreignTableId);
			aff.setCustomNum(" "+customNoName);
			aff.setCustomId(customNoId);
			aff.setEventType(eventType);
			aff.setEventFunction(eventFunction);
			aff.setEventName(eventName);
			aff.setSfhebing(sfhebing);
			autoFormService.addFormFieldByTable(formTableName, aff,formId);
			out.print("{\"result\":\"success\"}");
		} catch (Exception e) {
			out.print("{\"result\":\"error\"}");
			e.printStackTrace();
		}
	}

	/**
	 * 根据表字段id查询对象
	 * @return
	 * @throws Exception
	 */
	public String getAutoFormFieldById() throws Exception{
		try {
			String autoFormFieldId = getRequest().getParameter("autoFormFieldId");
			String type = getRequest().getParameter("type"); //如果是1，则查看，2则修改
			String formTableName = getRequest().getParameter("formTableName");
			String formId = getRequest().getParameter("formId");
			AutoFormsField aff = autoFormService.getAutoFormFieldById(autoFormFieldId);
			getRequest().setAttribute("aff", aff);
			getRequest().setAttribute("autoFormFieldId", autoFormFieldId);
			if("1".equals(type)){
				return "toFieldView";
			}else{
				return "toFieldUpdate";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
	
	/**
	 * 修改
	 * @throws Exception
	 */
	public void updateAutoFormField() throws Exception{
		PrintWriter out = getResponse().getWriter();
		try {
			String autoFormFieldId = getRequest().getParameter("autoFormFieldId");
			AutoFormsField aff = autoFormService.getAutoFormFieldById(autoFormFieldId);
			aff.setFieldNote(fieldNote);
			aff.setIsReadOnly(isReadOnly);
			aff.setAuthenticationType(authenticationType);
			aff.setIsListShow(isListShow);
			aff.setIsPageShow(isPageShow);
			aff.setIsQueryField(isQueryField);
			aff.setShowSeq(showSeq);
			aff.setDefaultValue(defaultValue);
			aff.setFieldListWidth(fieldListWidth);
			aff.setIsOrderBy(isOrderBy);
			aff.setPageDisplayType(pageDisplayType);
			aff.setForeignCloumn(foreignCloumn);
			aff.setForeignDisCloumn(foreignDisCloumn);
			aff.setForeignTableName(foreignTableName);
			aff.setForeignTableId(foreignTableId);
			aff.setCustomNum(" "+customNoName);
			aff.setCustomId(customNoId);
			aff.setEventType(eventType);
			aff.setEventFunction(eventFunction);
			aff.setEventName(eventName);
			aff.setSfhebing(sfhebing);

			autoFormService.updateAutoFormField(aff);
			out.print("{\"result\":\"success\"}");
		} catch (Exception e) {
			out.print("{\"result\":\"error\"}");
			e.printStackTrace();
		}
	}
	
	/**
	 * 删除表字段
	 * @throws Exception
	 */
	public void deleteFormFieldById() throws Exception{
		PrintWriter out = getResponse().getWriter();
		try {
			String autoFormFieldId = getRequest().getParameter("autoFormFieldId");
			String tableName = getRequest().getParameter("formTableName");
			String columnName = getRequest().getParameter("columnName");
			autoFormService.deleteAutoFormFieldBeanAndTableColumn(autoFormFieldId, tableName, columnName);
			out.print("{\"result\":\"success\"}");
		} catch (Exception e) {
			e.printStackTrace();
			out.print("{\"result\":\"error\"}");
		}
	}
	
	/**
	 * 批量页面显示隐藏
	 * @throws Exception
	 */
	public void batchPageDisplay() throws Exception{
		PrintWriter out = getResponse().getWriter();
		try {
			//操作类型 0：页面隐藏，1：页面显示，2：列表隐藏，3：列表显示
			String operationType = getRequest().getParameter("operationType");
			String formfieldids = getRequest().getParameter("formfieldids");
			String [] fieldList = formfieldids.split(",");
			for (String fieldId : fieldList) {
				AutoFormsField aff = autoFormService.getAutoFormFieldById(fieldId);
				if("1".equals(operationType)){
					aff.setIsPageShow(1l);
				}else if("0".equals(operationType)){
					aff.setIsPageShow(0l);
				}else if("2".equals(operationType)){
					aff.setIsListShow(0l);
				}else{
					aff.setIsListShow(1l);
				}
				autoFormService.updateAutoFormField(aff);
			}
			out.print("{\"result\":\"success\"}");
		} catch (Exception e) {
			out.print("{\"result\":\"error\"}");
			e.printStackTrace();
		}
	}
	
	/**
	 * 查询按钮列表
	 * @throws Exception
	 */
	public void queryAutoFormButtonList() throws Exception{
		try {
			
			Map<String, String> filterMap = getPagerMap();
			String thisTableNameId = getRequest().getParameter("thisTableNameId");
			//将查询条件保存到Map里
			filterMap.put("this_table_name_id", thisTableNameId);
			String[] viewArray = { "ID", "button_name","this_table_name_id","this_table_name","this_table_column","by_table_name_id","by_table_name","by_table_column","button_type:[{'0':'按钮','1':'列表','2':'原始记录','3':'合格证','4':'记录单','5':'军检原始记录'}]","operate_type:[{'0':'编辑','1':'查看'}]"};
			// 查询所有的数据
			PageResult result = autoFormService.queryAutoFormsButtonList(filterMap);
			String returnStr = getColumnJson(result, viewArray);
			setContentType();
			PrintWriter out = getResponse().getWriter();
			System.out.println(returnStr);
			out.print(returnStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 添加按钮
	 * @throws Exception
	 */
	public void saveAutoFormButton() throws Exception{
		PrintWriter out = getResponse().getWriter();
		try {
			AutoFormsButton afb = new AutoFormsButton();
			afb.setButtonName(buttonName);
			afb.setByTableNameId(byTableNameId);
			afb.setByTableColumn(byTableColumn);
			afb.setThisTableColumn(thisTableColumn);
			afb.setThisTableNameId(thisTableNameId);
			if(!"".equals(fitterCondition) && fitterCondition != null){
				afb.setFitterCondition(fitterCondition);
			}else{
				afb.setFitterCondition("");
			}
			afb.setButtonType(buttonType);
			afb.setOperateType(operateType);
			autoFormService.saveAutoFormsButton(afb);
			out.print("{\"result\":\"success\"}");
		} catch (Exception e) {
			out.print("{\"result\":\"error\"}");
			e.printStackTrace();
		}
	}
	
	/**
	 * 修改按钮
	 * @return
	 * @throws Exception
	 */
	public String initUpdateAutoFormButton() throws Exception{
		String buttonId = getRequest().getParameter("butttonId");
		String buttonSql = " select t.id,t.button_name,t.by_table_column,t.fitter_condition,t.by_table_name_id,t1.form_cn_name as t1formcnname,"+
						   " t.this_table_name_id,t2.form_cn_name as t2formcnname,t.this_table_column,t.button_type,t.operate_type,t1.form_en_name  "+
						   " from auto_forms_button t  "+
						   " left join auto_forms_new t1 on t.by_table_name_id = t1.id "+
						   " left join auto_forms_new t2 on t.this_table_name_id = t2.id where t.id = "+buttonId;
		List list  = autoFormService.getDatabySql(buttonSql);
		Object[] buttonObj = null;
		if(list.size() > 0){
			buttonObj = (Object[])list.get(0);
		}
		getRequest().setAttribute("buttonObj", buttonObj);
		return "initUpdateButton";
	}
	
	/**
	 * 修改提交
	 * @throws Exception
	 */
	public void submitUpdateAutoFormButton() throws Exception{
		PrintWriter out = getResponse().getWriter();
		String buttonId = getRequest().getParameter("buttonId");
		try {
			AutoFormsButton afb = autoFormService.getAutoFormButtonById(buttonId);
			afb.setButtonName(buttonName);
			if(byTableNameId != null && !"".equals(byTableNameId) && !"null".equals(byTableNameId)){
				afb.setByTableNameId(byTableNameId);
			}
			afb.setByTableColumn(byTableColumn);
			afb.setThisTableColumn(thisTableColumn);
			if(thisTableNameId != null && !"".equals(thisTableNameId) && !"null".equals(thisTableNameId)){
				afb.setThisTableNameId(thisTableNameId);
			}
			afb.setFitterCondition(fitterCondition);
			afb.setButtonType(buttonType);
			afb.setOperateType(operateType);
			autoFormService.updateAutoFormButton(afb);
			out.print("{\"result\":\"success\"}");
		} catch (Exception e) {
			out.print("{\"result\":\"error\"}");
			e.printStackTrace();
		}
	}
	
	
	
	/**
	 * 删除按钮
	 * @throws Exception
	 */
	public void deleteAutoFormsButtonById() throws Exception{
		PrintWriter out = getResponse().getWriter();
		try {
			String buttonId = getRequest().getParameter("buttonId");
			autoFormService.deleteAutoFormsButtonById(buttonId);
			out.print("{\"result\":\"success\"}");
		} catch (Exception e) {
			e.printStackTrace();
			out.print("{\"result\":\"error\"}");
		}
	}
	
	/**
	 * 查询编号规则列表
	 * @throws Exception
	 */
	public void queryCunstomNoList() throws Exception{
		try {
			
			Map<String, String> filterMap = getPagerMap();
			String thisTableNameId = getRequest().getParameter("thisTableNameId");
			//将查询条件保存到Map里
			filterMap.put("this_table_name_id", thisTableNameId);
			String[] viewArray = { "id","custom_name","custom_num","cur_number"};
			// 查询所有的数据
			PageResult result = autoFormService.queryCustomNoList(filterMap);
			String returnStr = getColumnJson(result, viewArray);
			setContentType();
			PrintWriter out = getResponse().getWriter();
			System.out.println(returnStr);
			out.print(returnStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 保存编号规则
	 * @throws Exception
	 */
	public void saveCustomNo() throws Exception{
		PrintWriter out = getResponse().getWriter();
		String customNum = getRequest().getParameter("customNum");
		String customName = getRequest().getParameter("customName");
		String index = "";
		try {
			TdmCustomNo tcn = new TdmCustomNo();
			tcn.setCustomNum(customNum);
			tcn.setCustomName(customName);
			tcn.setCurNumber(0l);
			autoFormService.saveCustomNo(tcn);
			out.print("{\"result\":\"success\"}");
		} catch (Exception e) {
			out.print("{\"result\":\"error\"}");
			e.printStackTrace();
		}
	}
	/**
	 * 修改保存编号规则
	 * @throws Exception
	 */
	public void updateCustomNo() throws Exception{
		PrintWriter out = getResponse().getWriter();
		String customName = getRequest().getParameter("customName");
		String customNum = getRequest().getParameter("customNum");
		String curNumber = getRequest().getParameter("curNumber");
		String id = getRequest().getParameter("id");
		String index = "";
		try {
			TdmCustomNo tcn = autoFormService.queryTdmCustomNo(Long.parseLong(id));
			tcn.setCustomName(customName);
			tcn.setCurNumber(Long.parseLong(curNumber));
			autoFormService.updateCustomNo(tcn);
			out.print("{\"result\":\"success\"}");
		} catch (Exception e) {
			out.print("{\"result\":\"error\"}");
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取规则编号
	 * author:lishu	 2014-4-10
	 */
	public void queryCustomNo() throws Exception{
		setContentType();
		PrintWriter out = getResponse().getWriter();
		String custonNumber = getRequest().getParameter("custonNumber");
		String id = getRequest().getParameter("id");
		try {
			String no = autoFormService.queryCunstomNumber(custonNumber, Long.parseLong(id));
			out.print("{\"result\":\"success\",\"no\":\""+no+"\"}");
		} catch (Exception e) {
			out.print("{\"result\":\"error\"}");
			e.printStackTrace();
		}
	}
	
	public void savePriority() throws Exception{
		PrintWriter out = getResponse().getWriter();
		try {
			String ids = getRequest().getParameter("ids");
			autoFormService.savePriority(ids,"");
			out.print("{\"result\":\"success\"}");
		} catch (Exception e) {
			e.printStackTrace();
			out.print("{\"result\":\"error\"}");
		}
	}
	public IAutoFormService getAutoFormService() {
		return autoFormService;
	}

	public void setAutoFormService(IAutoFormService autoFormService) {
		this.autoFormService = autoFormService;
	}

	public String getFormCnName() {
		return formCnName;
	}

	public void setFormCnName(String formCnName) {
		this.formCnName = formCnName;
	}

	public String getFormEnName() {
		return formEnName;
	}

	public void setFormEnName(String formEnName) {
		this.formEnName = formEnName;
	}

	public String getFormPath() {
		return formPath;
	}

	public void setFormPath(String formPath) {
		this.formPath = formPath;
	}

	public String getFormNote() {
		return formNote;
	}

	public void setFormNote(String formNote) {
		this.formNote = formNote;
	}

	public Long getFormColumn() {
		return formColumn;
	}

	public void setFormColumn(Long formColumn) {
		this.formColumn = formColumn;
	}

	public String getFormFormat() {
		return formFormat;
	}

	public void setFormFormat(String formFormat) {
		this.formFormat = formFormat;
	}

	public Long getCreateNameId() {
		return createNameId;
	}

	public void setCreateNameId(Long createNameId) {
		this.createNameId = createNameId;
	}

	public String getCreateName() {
		return createName;
	}

	public void setCreateName(String createName) {
		this.createName = createName;
	}

	public Timestamp getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Timestamp createTime) {
		this.createTime = createTime;
	}

	public String getFormFormatDate() {
		return formFormatDate;
	}

	public void setFormFormatDate(String formFormatDate) {
		this.formFormatDate = formFormatDate;
	}

	public String getFormType() {
		return formType;
	}

	public void setFormType(String formType) {
		this.formType = formType;
	}

	public String getSuoslx() {
		return suoslx;
	}

	public void setSuoslx(String suoslx) {
		this.suoslx = suoslx;
	}

	public String getFormwidth() {
		return formwidth;
	}

	public void setFormwidth(String formwidth) {
		this.formwidth = formwidth;
	}

	public String getFormheight() {
		return formheight;
	}

	public void setFormheight(String formheight) {
		this.formheight = formheight;
	}

	public String getIscaiji() {
		return iscaiji;
	}

	public void setIscaiji(String iscaiji) {
		this.iscaiji = iscaiji;
	}

	public String getCaijiaddress() {
		return caijiaddress;
	}

	public void setCaijiaddress(String caijiaddress) {
		this.caijiaddress = caijiaddress;
	}

	public String getFormId() {
		return formId;
	}

	public void setFormId(String formId) {
		this.formId = formId;
	}

	public String getCnName() {
		return cnName;
	}

	public void setCnName(String cnName) {
		this.cnName = cnName;
	}

	public String getFieldName() {
		return fieldName;
	}

	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}

	public String getFieldNote() {
		return fieldNote;
	}

	public void setFieldNote(String fieldNote) {
		this.fieldNote = fieldNote;
	}

	public String getFieldType() {
		return fieldType;
	}

	public void setFieldType(String fieldType) {
		this.fieldType = fieldType;
	}

	public Long getIsReadOnly() {
		return isReadOnly;
	}

	public void setIsReadOnly(Long isReadOnly) {
		this.isReadOnly = isReadOnly;
	}

	public String getAuthenticationType() {
		return authenticationType;
	}

	public void setAuthenticationType(String authenticationType) {
		this.authenticationType = authenticationType;
	}

	public Long getIsListShow() {
		return isListShow;
	}

	public void setIsListShow(Long isListShow) {
		this.isListShow = isListShow;
	}

	public Long getIsPageShow() {
		return isPageShow;
	}

	public void setIsPageShow(Long isPageShow) {
		this.isPageShow = isPageShow;
	}

	public Long getShowSeq() {
		return showSeq;
	}

	public void setShowSeq(Long showSeq) {
		this.showSeq = showSeq;
	}

	public Long getIsColumnMerge() {
		return isColumnMerge;
	}

	public void setIsColumnMerge(Long isColumnMerge) {
		this.isColumnMerge = isColumnMerge;
	}

	public String getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}

	public Long getIsQueryField() {
		return isQueryField;
	}

	public void setIsQueryField(Long isQueryField) {
		this.isQueryField = isQueryField;
	}

	public Long getFieldLength() {
		return fieldLength;
	}

	public void setFieldLength(Long fieldLength) {
		this.fieldLength = fieldLength;
	}

	public String getFormTableName() {
		return formTableName;
	}

	public void setFormTableName(String formTableName) {
		this.formTableName = formTableName;
	}

	public Long getFieldListWidth() {
		return fieldListWidth;
	}

	public void setFieldListWidth(Long fieldListWidth) {
		this.fieldListWidth = fieldListWidth;
	}

	public Long getIsOrderBy() {
		return isOrderBy;
	}

	public void setIsOrderBy(Long isOrderBy) {
		this.isOrderBy = isOrderBy;
	}

	public Long getPageDisplayType() {
		return pageDisplayType;
	}

	public void setPageDisplayType(Long pageDisplayType) {
		this.pageDisplayType = pageDisplayType;
	}

	public String getForeignTableName() {
		return foreignTableName;
	}

	public void setForeignTableName(String foreignTableName) {
		this.foreignTableName = foreignTableName;
	}

	public String getForeignCloumn() {
		return foreignCloumn;
	}

	public void setForeignCloumn(String foreignCloumn) {
		this.foreignCloumn = foreignCloumn;
	}

	public String getForeignDisCloumn() {
		return foreignDisCloumn;
	}

	public void setForeignDisCloumn(String foreignDisCloumn) {
		this.foreignDisCloumn = foreignDisCloumn;
	}

	public Long getForeignTableId() {
		return foreignTableId;
	}

	public void setForeignTableId(Long foreignTableId) {
		this.foreignTableId = foreignTableId;
	}

	public String getButtonName() {
		return buttonName;
	}

	public void setButtonName(String buttonName) {
		this.buttonName = buttonName;
	}

	public String getByTableNameId() {
		return byTableNameId;
	}

	public void setByTableNameId(String byTableNameId) {
		this.byTableNameId = byTableNameId;
	}

	public String getByTableColumn() {
		return byTableColumn;
	}

	public void setByTableColumn(String byTableColumn) {
		this.byTableColumn = byTableColumn;
	}

	public String getThisTableNameId() {
		return thisTableNameId;
	}

	public void setThisTableNameId(String thisTableNameId) {
		this.thisTableNameId = thisTableNameId;
	}

	public String getThisTableColumn() {
		return thisTableColumn;
	}

	public void setThisTableColumn(String thisTableColumn) {
		this.thisTableColumn = thisTableColumn;
	}

	public String getFitterCondition() {
		return fitterCondition;
	}

	public void setFitterCondition(String fitterCondition) {
		this.fitterCondition = fitterCondition;
	}

	public Long getButtonType() {
		return buttonType;
	}

	public void setButtonType(Long buttonType) {
		this.buttonType = buttonType;
	}

	public Long getOperateType() {
		return operateType;
	}

	public void setOperateType(Long operateType) {
		this.operateType = operateType;
	}
	public String getColumnCnName() {
		return columnCnName;
	}
	public void setColumnCnName(String columnCnName) {
		this.columnCnName = columnCnName;
	}
	public String getColumnSql() {
		return columnSql;
	}
	public void setColumnSql(String columnSql) {
		this.columnSql = columnSql;
	}
	public String getColumnWidth() {
		return columnWidth;
	}
	public void setColumnWidth(String columnWidth) {
		this.columnWidth = columnWidth;
	}
	public String getColumnForigen() {
		return columnForigen;
	}
	public void setColumnForigen(String columnForigen) {
		this.columnForigen = columnForigen;
	}
	public String getSpecialColumn() {
		return specialColumn;
	}
	public void setSpecialColumn(String specialColumn) {
		this.specialColumn = specialColumn;
	}
	public String getOtherColumnSql() {
		return otherColumnSql;
	}
	public void setOtherColumnSql(String otherColumnSql) {
		this.otherColumnSql = otherColumnSql;
	}
	public Long getSpecialType() {
		return specialType;
	}
	public void setSpecialType(Long specialType) {
		this.specialType = specialType;
	}
	public String getCustomNoName() {
		return customNoName;
	}
	public void setCustomNoName(String customNoName) {
		this.customNoName = customNoName;
	}
	public Long getCustomNoId() {
		return customNoId;
	}
	public void setCustomNoId(Long customNoId) {
		this.customNoId = customNoId;
	}
	public String getEventType() {
		return eventType;
	}
	public void setEventType(String eventType) {
		this.eventType = eventType;
	}
	public String getEventFunction() {
		return eventFunction;
	}
	public void setEventFunction(String eventFunction) {
		this.eventFunction = eventFunction;
	}
	public String getEventName() {
		return eventName;
	}
	public void setEventName(String eventName) {
		this.eventName = eventName;
	}
	public String getColumnguolvsql() {
		return columnguolvsql;
	}
	public void setColumnguolvsql(String columnguolvsql) {
		this.columnguolvsql = columnguolvsql;
	}
	public Long getSfhebing() {
		return sfhebing;
	}
	public void setSfhebing(Long sfhebing) {
		this.sfhebing = sfhebing;
	}
	public String getMapmark() {
		return mapmark;
	}
	public void setMapmark(String mapmark) {
		this.mapmark = mapmark;
	}
	
}
